import QtQuick 2.0
import QtQuick.Controls 1.1
import org.qtproject.qtquick 1.0

import "qrc:/js/resources/Constants.js" as Constant

import "../delegate"
import "../common"
import "../page"
import "../configure"


MainBasePage {
    id: id_chatPage

    width: Constant.width * m_ratio
    height: Constant.height * m_ratio

    m_header.title: Constant.m_displayNameFriendActive
    m_header.visibleAvatarFriend: true
    m_header.showBack: true;
    m_header.onBackClicked: {
        console.log("m_header.onBackClicked: ");
        id_pageView.pop();
        id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/page/MainPage.qml"));
    }


    property var m_userId: Constant.m_uidActive;
    property var m_friendId: Constant.m_fidActive;

    property real m_ratio : ApplicationInfo.ratio;

    property int m_margin: 10 * m_ratio;

    function positionViewAtIndexMessage() {
//        if (id_chatList.count > 10) {
//            id_chatList.positionViewAtIndex(0, ListView.Beginning)
//        }
    }

    Item {
        id: id_chatPageItem

        width: parent.width
        height: parent.height - m_header.height - id_rowChatInput.height

        anchors.top: m_header.bottom;


        Column {
            spacing: 10 * m_ratio

            width: parent.width
            height: parent.height


            ListView {
                id: id_chatList
                width: parent.width
                height: parent.height - id_rowChatInput.height

                model: ApplicationInfo.getConversationModel(m_friendId)

                delegate: ConversationDelegate {

                }

                verticalLayoutDirection: ListView.BottomToTop

                spacing: 5 * m_ratio

                section.property: "datetimeCombination"
                section.criteria: ViewSection.FullString
                section.delegate: CombinationDateTime { }

            }

            Row {
                id: id_rowChatInput
                width: parent.width - 50 * m_ratio
                height: 50 * m_ratio
                spacing: 10 * m_ratio

                anchors.horizontalCenter: parent.horizontalCenter

                TextArea {
                    id: id_txtInputChat

                    width: parent.width - id_bttnLogin.width
                    height: parent.height

//                    Keys.onEnterPressed: {
//                        ApplicationInfo.onSendMessage(id_txtInputChat.text);
//                        id_txtInputChat.text = "";

//                        Qt.inputMethod.hide();
//                    }

                }

                Button {
                    id: id_bttnLogin
                    text: "Chat";
                    width: 100 * m_ratio
                    height: 50  * m_ratio



                    anchors.verticalCenter: parent.verticalCenter

                    onClicked: {
                        ApplicationInfo.onSendMessage(id_txtInputChat.text);
                        id_txtInputChat.text = "";

                        Qt.inputMethod.hide();
                    }
                }
            }
        }
    }

    Component.onCompleted:{
        ApplicationInfo.positionViewAtIndexMessage.connect(positionViewAtIndexMessage);
    }
}
